package com.widget;

import java.util.LinkedList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.util.DensityUtil;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.View;

@SuppressLint("DrawAllocation")
public class AverageView extends View {
	Context mContext;
	
	public AverageView(Context context) {
		super(context);
		mContext = context;
	}
	
	public AverageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
	}

	public AverageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if(mJSONArray == null) return;

		Paint p = new Paint();
		p.setAntiAlias(true);
		p.setStyle(Paint.Style.FILL);
		p.setColor(Color.parseColor("#50E19A47"));
		
		Path path2=new Path();
		JSONArray mArray = mJSONArray.getJSONObject(mIndex).getJSONArray("data");
		
		int count = mArray.size();
		if(count == 1)
		{
			int goal = mArray.getJSONObject(0).getIntValue("average_min");
			int yPos = mHeight - goal*mHeight/mMilk;
			
			int goalM = mArray.getJSONObject(0).getIntValue("average_max");
			int yPosM = mHeight - goalM*mHeight/mMilk;

			path2.moveTo(0, yPos);
			path2.quadTo(0, yPos, mWidth, yPos); 
			path2.quadTo(mWidth, yPos, mWidth, yPosM); 
			path2.quadTo(mWidth, yPosM, 0, yPosM); 
		}
		else
		{
			int xPos = 0;
			int yPos = 0;
			int offset = mWidth/(count - 1);
			
			for(int i = 0; i < count; i++)
			{
				int goal = mArray.getJSONObject(i).getIntValue("average_min");
				int height = mHeight - goal*mHeight/mMilk;
				
				if(i == 0)
				{
					path2.moveTo(0, height);
					xPos = i*offset;
				}
				else if(count - 1 == i)
				{
					path2.quadTo(xPos, yPos, mWidth, height); 
					xPos = mWidth;
				}
				else
				{
					path2.quadTo(xPos, yPos, i*offset, height); 
					xPos = i*offset;
				}
				
				yPos = height;
			}
			
			for(int i = count - 1; i >= 0; i--)
			{
				int goal = mArray.getJSONObject(i).getIntValue("average_max");
				int height = mHeight - goal*mHeight/mMilk;
				
				if(count - 1 == i)
				{
					path2.quadTo(mWidth, yPos, mWidth, height); 
					xPos = mWidth;
				}
				else
				{
					path2.quadTo(xPos, yPos, i*offset, height); 
					xPos = i*offset;
				}
				yPos = height;
				
				if(i == 0)
				{
					goal = mArray.getJSONObject(i).getIntValue("average_min");
					height = mHeight - goal*mHeight/mMilk;
					
					path2.quadTo(xPos, yPos, 0, height); 
				}
			}
		}
		
		canvas.drawPath(path2, p);//画出贝塞尔曲线
	}
	

	JSONArray mJSONArray = null;
	int mIndex = 0;
	
	int mHeight;
	int mWidth;
	int mMilk;
	
	public void setPoint(JSONArray jsonArray,int height,int width,int milk,int index)
	{
		mJSONArray = jsonArray;
		mHeight = height;
		mWidth = width;
		mMilk = milk;
		mIndex = index;
		
		invalidate();
	}
}
